//
//  VCOfSecond.m
//  05_变换
//
//  Created by apple on 16/5/30.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "VCOfSecond.h"
#import <GLKit/GLKit.h>


@interface VCOfSecond ()

@property (weak, nonatomic) IBOutlet UIView *layerView1;

@property (weak, nonatomic) IBOutlet UIView *layerView2;

@property (weak, nonatomic) IBOutlet UIView *cubeView;

@property (strong,nonatomic) IBOutletCollection(UIView) NSArray *viewss;

@end



@implementation VCOfSecond

- (void)viewDidLoad{
    
    [super viewDidLoad];

    [self atrributeOfDoubleSided];
    
    [self makeCube];


}

/**背面 (DoubleSided)*/
- (void)atrributeOfDoubleSided{

    //将图片旋转180度 , 将得到一个镜像对称的图片
    //doubleSided : 可以将背面渲染关闭 , 节省GPU
    //??????X轴旋转时 , 正值 , 好像是逆时针旋转 . (需求证)
    
    UIImage *image = [UIImage imageNamed:@"pic_s1.jpg"];
    
    self.layerView1.layer.contents = (id)image.CGImage;
    self.layerView1.layer.transform = CATransform3DMakeRotation(M_PI, 0, 1, 0);
    
    
    self.layerView2.layer.contents = (id)image.CGImage;
    self.layerView2.layer.transform = CATransform3DMakeRotation(M_PI, 0, 1, 0);
    self.layerView2.layer.borderWidth = 1;
    self.layerView2.layer.doubleSided = NO;

}


/**扁平化图层 */
    //我们看到的3D空间不一定都存在于 同一个3D空间
    //每个图层的3D场景其实是扁平化的
    //父视图将其子视图 都扁平化了.



/**固话对象 (创建正方体)*/

- (void)makeCube{

    CATransform3D superTransform = CATransform3DIdentity;
    superTransform.m34 = -1.0 / 500.0;
    
    //旋转相机
    superTransform = CATransform3DRotate(superTransform, M_PI_4, -1, 0, 0);
    superTransform = CATransform3DRotate(superTransform, M_PI_4, 0, -1, 0);
    
    self.cubeView.layer.sublayerTransform = superTransform;

    CATransform3D transform = CATransform3DMakeTranslation(45, 0, 0);
    transform = CATransform3DRotate(transform, M_PI_2, 0, 1, 0);
    [self addFace:0 WithTransform:transform];
    
    transform = CATransform3DMakeTranslation(0, 0, 45);
    transform = CATransform3DRotate(transform, M_PI_2, 0, 0, 0);
    [self addFace:1 WithTransform:transform];
    
    transform = CATransform3DMakeTranslation(-45, 0, 0);
    transform = CATransform3DRotate(transform, M_PI_2, 0, -1, 0);
    [self addFace:2 WithTransform:transform];
    
    transform = CATransform3DMakeTranslation(0, 0, -45);
    transform = CATransform3DRotate(transform, M_PI, 0, 1, 0);
    [self addFace:3 WithTransform:transform];
    
    transform = CATransform3DMakeTranslation(0, 45, 0);
    transform = CATransform3DRotate(transform, M_PI_2, -1, 0, 0);
    [self addFace:4 WithTransform:transform];

    transform = CATransform3DMakeTranslation(0, -45, 0);
    transform = CATransform3DRotate(transform, M_PI_2, 1, 0, 0);
    [self addFace:5 WithTransform:transform];


    


}


- (void)addFace:(NSInteger)index WithTransform:(CATransform3D)transform{

    UIView *view = self.viewss[index];
    
    view.layer.transform = transform;
    
    [self applyLightingToFace:view.layer];

}



/** 光亮和阴影  */
//需导入头文件 <GLKit/GLKit.h> !!!!!

//如果需要动态的创建光线效果.
//对每个视图应用不同的alpha值, 做出半透明的阴影效果.(需得到每个面的正太向量)
//根据想象的光源计算出 两个向量叉乘,叉乘代表了光源和图层的角度, 决定了图层有多大程度上的光亮
//图层的CATransform3D 转换成 GLKMatrix4 , 通过GLKMatrix4GetMatrix3函数 得到3*3旋转矩阵
// 用 LIGHT_DIRECTION 和 AMBIENT_LIGHT 的值来切换光线效果

- (void)applyLightingToFace:(CALayer *)face{

    CALayer *layer = [CALayer layer];
    layer.frame = face.bounds;
    [face addSublayer:layer];

    CATransform3D transform = face.transform;
    GLKMatrix4 matrix4 = *(GLKMatrix4 *)&transform;
    GLKMatrix3 matrix3 = GLKMatrix4GetMatrix3(matrix4);
    //get face normal
    GLKVector3 normal = GLKVector3Make(0, 0, 1);
    normal = GLKMatrix3MultiplyVector3(matrix3, normal);
    normal = GLKVector3Normalize(normal);
    //get dot product with light direction
    GLKVector3 light = GLKVector3Normalize(GLKVector3Make(0,1,-0.5));
    float dotProduct = GLKVector3DotProduct(light, normal);
    //set lighting layer opacity
    CGFloat shadow = 1 + dotProduct - 0.5;
    UIColor *color = [UIColor colorWithWhite:0 alpha:shadow];
    layer.backgroundColor = color.CGColor;
    

}

/** 点击事件 */
//需要注意的是 , 不管在3D中 显示的视图顺序是什么样子,
//但点击事件的顺序是由在父视图添加顺序来决定的

//如果想让 顶视图触发点击事件 , 可以通过调整添加的视图顺序 和 关闭其他视图的用户交互来实现.



- (IBAction)clickButton:(UIButton *)sender {
    
    sender.backgroundColor = [UIColor cyanColor];


}



@end
